package com.showboom.elauncher.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;

import com.showboom.elauncher.MyAppliacation;
import com.showboom.elauncher.activity.MainActivity;
import com.showboom.elauncher.util.HmSharedPreferencesUtils;
import com.showboom.elauncher.util.Util;

public class WaKuangStatusReceiver extends BroadcastReceiver {

    public static final String ACTION_SIGNIN = "com.haoyou.msg.SIGNIN";
    public static final String ACTION_AUTHENTICATION = "com.haoyou.msg.AUTHENTICATION";
    public static final String ACTION_ERROR = "com.haoyou.msg.EXCEPTION";
    public static final String ACTION_SIGNIN_SHOWBOOM = "com.showboom.SIGNIN";

    public static final int MESSAGE_ERROR = 404;
    public static final int MESSAGE_PASSWORD_ERROR = 401;
    public static final int MESSAGE_SIGNIN_STATUS = 101;
    public static final int MESSAGE_AUTHENTICATION = 102;

    Handler mHandler;

    public WaKuangStatusReceiver(Handler handler) {
        mHandler = handler;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d("leihuiming", "action = " + action);

        switch (action) {
            case ACTION_SIGNIN:
                if(MainActivity.isFirstOpen()) return;
                boolean newSigninStatus = intent.getBooleanExtra("signin", false);
                MyAppliacation.signinStatus = newSigninStatus;
                HmSharedPreferencesUtils.setParam("signin", newSigninStatus);
                mHandler.sendEmptyMessage(MESSAGE_SIGNIN_STATUS);
                break;
            case ACTION_AUTHENTICATION:
                boolean newAuthStatus = intent.getBooleanExtra("authentication", false);
                Log.d("leihuiming", "newAuthStatus = " + newAuthStatus);
                MyAppliacation.authStatus = newAuthStatus;
                HmSharedPreferencesUtils.setParam("authentication", newAuthStatus);
                if(MainActivity.isFirstOpen()) return;
                mHandler.sendEmptyMessage(MESSAGE_AUTHENTICATION);

                break;
            case ACTION_ERROR:
                String reason = intent.getStringExtra("exception");
                if (reason != null && reason.equals("pass_error")) {
                    HmSharedPreferencesUtils.setParam("authentication", false);
                    mHandler.sendEmptyMessage(MESSAGE_AUTHENTICATION);
                } else {
                    mHandler.sendEmptyMessage(MESSAGE_ERROR);
                }
                break;
            case Intent.ACTION_SCREEN_OFF:
                Util.screenOn();
                break;
            case ACTION_SIGNIN_SHOWBOOM:
                boolean newStatus = intent.getBooleanExtra("signin", false);
                MyAppliacation.signinStatus = newStatus;
                HmSharedPreferencesUtils.setParam("signin", newStatus);
                if(MainActivity.isFirstOpen() && (newStatus == false)) return;
                mHandler.sendEmptyMessage(MESSAGE_SIGNIN_STATUS);
                break;
            default:
                break;
        }

    }

}
